﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using MahApps.Metro.Controls;
using Microsoft.Practices.Unity;
using SpectrumAnalyser.Data.Files.SpectrumDatabaseContracts.Repository;
using SpectrumAnalyser.Shell.ViewModels;

namespace SpectrumAnalyser.Shell.Controls.SpectrumDbConnections
{
    /// <summary>
    /// Interaction logic for ManageDbConnections.xaml
    /// </summary>
    public partial class ManageSpectrumDbConnectionsWindow : MetroWindow
    {
        public ManageSpectrumDbConnectionsWindow(IUnityContainer unityContainer)
        {
            InitializeComponent();

            var connections = unityContainer.ResolveAll<ISpectrumLinesRepository>();

            ConnectionsTypeCombo.Items.Clear();
            ConnectionsTypeCombo.ItemsSource = connections.Select(x => new CreateInstanceViewModel
                {
                    DisplayName = x.ConnectionType,
                    InstanceType = x.GetType()
                });
        }

        public IList<SpectrumDbConnectionViewModel> Connections
        {
            get { return ExistingConnections.Items.Cast<SpectrumDbConnectionViewModel>().ToList(); }
            set
            {
                foreach (var spectrumDbConnection in value)
                {
                    ExistingConnections.Items.Add(spectrumDbConnection);
                }
            }
        }

        private void AddConnection(object sender, RoutedEventArgs e)
        {
            var connection = ConnectionsTypeCombo.SelectedItem as CreateInstanceViewModel;
            if (ReferenceEquals(connection, null))
            {
                return;
            }

            var instance = new SpectrumDbConnectionViewModel
                {
                    ConnectionName = string.Format("Default Connection {0}",
                                                   ExistingConnections.Items.Count + 1),
                    Type = connection.InstanceType
                };

            PropertyGrid.SelectedObject = instance;

            ExistingConnections.Items.Insert(0, instance);
        }

        private void RemoveConnection(object sender, RoutedEventArgs e)
        {
            var selectedItem = ExistingConnections.SelectedItem;
            if (!ReferenceEquals(selectedItem, null))
            {
                ExistingConnections.Items.Remove(selectedItem);
            }
        }

        private void ExistingConnectionsSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PropertyGrid.SelectedObject = ExistingConnections.SelectedItem;
        }

        public event RoutedEventHandler OnSubmit;

        private void Ok(object sender, RoutedEventArgs e)
        {
            if(!ReferenceEquals(OnSubmit,null))
            {
                //OnSubmit(this, e);
            }
            DialogResult = true;
            Close();
        }

        private void Cancel(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}
